/*----------------------------------------------------------------------
  - File name      : STC8Ax_I2C.c
  - Author         : Quark Team
  - Update date    : 2020-09-10                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: Layout module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "STC8Ax_I2C.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/
/* None */
/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: I2C delay function                       |
| @param      : uint8 nus                                |
| @return     : None                                     |
--------------------------------------------------------*/

static void I2C_Delay_us(uint8 nus)
{
	uint8 i;
	while(nus--)
	{
		i = 6;
		while (--i);
	}  
}

/*--------------------------------------------------------
| @Description: I2C wait time function                   |
| @param      : None                                     |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

static FSCSTATE I2C_Wait(void)
{
    uint8 count = 0;
    while (!(I2CMSST & 0x40))
    {
        I2C_Delay_us(1);
        if(count++ >= 30) //等待超过30us，则退出等待。
        {
						count = 0;
            return FSC_FAIL;
        }
    }
    I2CMSST &= 0xBF;
    return FSC_SUCCESS;  
}

/*--------------------------------------------------------
| @Description: I2C init function                        |
| @param      : I2CType_Type Type, uint8 WTime,          |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE I2C_Init(I2CType_Type Type, uint8 WTime, FUNSTATE State)
{
  if(WTime <= 0x3F)
  {
    EAXFR_ENABLE();
    I2CCFG = (I2CCFG & 0xBF) | (Type << 6);
    I2CCFG = (I2CCFG & 0xC0) | (WTime);
    I2CCFG = (I2CCFG & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: I2C start function                       |
| @param      : None                                     |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE I2C_Start(void)
{
  I2CMSCR = 0x01;    
  return I2C_Wait();
}

/*--------------------------------------------------------
| @Description: I2C Stop function                        |
| @param      : None                                     |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE I2C_Stop(void)
{
  I2CMSCR = 0x06;                          
  return I2C_Wait();
}

/*--------------------------------------------------------
| @Description: I2C send ack function                    |
| @param      : None                                     |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE I2C_Send_ACK(void)
{
  I2CMSST = 0x00;                            
  I2CMSCR = 0x05;    
  return I2C_Wait();
}

/*--------------------------------------------------------
| @Description: I2C send nak function                    |
| @param      : None                                     |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE I2C_Send_NAK(void)
{
  I2CMSST = 0x01;                          
  I2CMSCR = 0x05;                           
  I2C_Wait();
  return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: I2C read Ack function                    |
| @param      : None                                     |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE I2C_Read_ACK(void)
{
  I2CMSCR = 0x03;                             
  return I2C_Wait();
}

/*--------------------------------------------------------
| @Description: I2C send data function                   |
| @param      : None                                     |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE I2C_Send_Data(uint8 Dat)
{
  I2CTXD = Dat;                            
  I2CMSCR = 0x02;                          
  return I2C_Wait();
}

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/



